package com.lyz.myself.controller;

import com.lyz.myself.config.utils.Result;
import com.lyz.myself.config.utils.base.CodeMsg;
import com.lyz.myself.config.utils.oss.AliyunOSSUtil;
import com.lyz.myself.config.utils.oss.ConstantProperties;
import com.lyz.myself.config.utils.util.DateUtil;
import com.lyz.myself.pojo.Po.FileLeads;
import com.lyz.myself.pojo.Po.RFileLeads;
import com.lyz.myself.service.FileService;
import com.lyz.myself.service.RFileLeadsService;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class FileUploadController extends BaseController {

    @Autowired
    private FileService fileService;

    @Autowired
    RFileLeadsService rFileLeadsService;

    private static final Logger log = LoggerFactory.getLogger(FileUploadController.class);

    @ApiOperation("线索文件上传")
    @RequestMapping(value = {"/fileUpload"}, method = {RequestMethod.POST})
    public Result uploadFile(@RequestParam("files") MultipartFile[] files, @RequestParam Map<String, String> updateMap) {
        System.out.println(files.length);
        List<RFileLeads> listFile = new ArrayList<>();
        List<FileLeads> list = new ArrayList();
        List<Map<String, Object>> fileIdUrl = new ArrayList();
        FileLeads fileLeads = null;
        Map<String, Object> map = null;
        List<Integer> newList = new ArrayList<>();
        int insertFile = 0;
        try {
            if (files.length > 0) {
                List<String> fileList = new ArrayList();
                for (MultipartFile file : files) {
                    fileLeads = new FileLeads();
                    String filename = file.getOriginalFilename();
                    log.info("上传" + filename);
                    if (!"".equals(filename.trim())) {
                        File newFile = new File(ConstantProperties.PROP_UPLOAD_FOLDER + filename);
                        if (!newFile.getParentFile().exists()) {
                            newFile.getParentFile().mkdirs();
                        }
                        file.transferTo(newFile);

                        String[] url = AliyunOSSUtil.uploadObject2OSS(newFile);
                        fileList.add(AliyunOSSUtil.getUrl(ConstantProperties.OSS_BUCKET_NAME, url[1]));
                        newFile.delete();
                        fileLeads.setCreate_date(DateUtil.timeMillis());
                        fileLeads.setCompany_id(getUser().getCompany_id());
                        fileLeads.setRole_id(getUser().getRole_id());
                        fileLeads.setName(filename);
                        fileLeads.setType("file");
                        fileLeads.setBelongs(0);
                        Long size = Long.valueOf(file.getSize());
                        fileLeads.setSize(Integer.valueOf(size.intValue()));
                        fileLeads.setFile_path(AliyunOSSUtil.getUrl(ConstantProperties.OSS_BUCKET_NAME, url[1]));
                        list.add(fileLeads);
                        if (updateMap.get("leads_id") != null && updateMap.get("filePath") != null) {
                            System.out.println(files.length);
                            String fileId = fileService.findFiledId(updateMap.get("filePath"));
                            fileLeads.setFile_id(Integer.valueOf(fileId));
                            Map<String, Object> newMap = new HashMap<>();
                            newMap.put("name", filename);
                            newMap.put("size", Integer.valueOf(size.intValue()));
                            newMap.put("file_path", AliyunOSSUtil.getUrl(ConstantProperties.OSS_BUCKET_NAME, url[1]));
                            newMap.put("leads_id", Integer.valueOf(updateMap.get("leads_id")));
                            newMap.put("filePath", updateMap.get("filePath"));
                            insertFile = fileService.updateFiled(newMap);
                        } else {
                            insertFile = this.fileService.insert(list);
                        }
                        newList.add(fileLeads.getFile_id());
                    }
                }
                for (int j = 0; j < fileList.size(); j++) {
                    Map<String, Object> newMap = new HashMap<>();
                    newMap.put("file_path", fileList.get(j));
                    newMap.put("file_id", newList.get(j));
                    fileIdUrl.add(newMap);
                }
                if (updateMap.get("leads_id") != null) {
                    for (int i = 0; i < fileList.size(); i++) {
                        RFileLeads rFileLeads = new RFileLeads();
                        rFileLeads.setFile_id(newList.get(i));
                        System.out.println("");
                        rFileLeads.setLeads_id(Integer.parseInt(updateMap.get("leads_id")));
                        rFileLeads.setCompany_id(getUser().getCompany_id());
                        listFile.add(rFileLeads);
                    }
                    int insertFocus = rFileLeadsService.insertFile(listFile);
                    if (insertFocus > 0) {
                        return Result.success(fileIdUrl, "添加成功！");
                    } else {
                        return Result.error(CodeMsg.ERROR, "添加失败");
                    }
                }
                if (insertFile > 0) {
                    return Result.success(fileIdUrl, "成功！");
                } else {
                    return Result.error(CodeMsg.ERROR, "失败");
                }
            }
        } catch (Exception e) {
            log.error(e.getMessage());
            return Result.error(CodeMsg.ERROR, e.getMessage());
        }
        return Result.error(CodeMsg.ERROR, "文件为空");
    }

    @ApiOperation("线索图片上传")
    @RequestMapping(value = {"/fileImage"}, method = {RequestMethod.POST})
    public Result fileImage(@RequestParam("files") MultipartFile[] files, @RequestParam Map<String, String> updateMap) {
        System.out.println(files.length);
        List<RFileLeads> listFile = new ArrayList<>();
        List<FileLeads> list = new ArrayList();
        List<Map<String, Object>> fileIdUrl = new ArrayList();
        FileLeads fileLeads = null;
        Map<String, Object> map = null;
        List<Integer> newList = new ArrayList<>();
        int insertFile = 0;
        try {
            if (files.length > 0) {
                List<String> fileList = new ArrayList();
                for (MultipartFile file : files) {
                    fileLeads = new FileLeads();
                    String filename = file.getOriginalFilename();
                    log.info("上传" + filename);
                    if (!"".equals(filename.trim())) {
                        File newFile = new File(ConstantProperties.PROP_UPLOAD_FOLDER + filename);
                        if (!newFile.getParentFile().exists()) {
                            newFile.getParentFile().mkdirs();
                        }
                        file.transferTo(newFile);

                        String[] url = AliyunOSSUtil.uploadObject2OSS(newFile);
                        fileList.add(AliyunOSSUtil.getUrl(ConstantProperties.OSS_BUCKET_NAME, url[1]));
                        newFile.delete();
                        fileLeads.setCreate_date(DateUtil.timeMillis());
                        fileLeads.setCompany_id(getUser().getCompany_id());
                        fileLeads.setRole_id(getUser().getRole_id());
                        fileLeads.setName(filename);
                        fileLeads.setType("img");
                        fileLeads.setBelongs(0);
                        Long size = Long.valueOf(file.getSize());
                        fileLeads.setSize(Integer.valueOf(size.intValue()));
                        fileLeads.setFile_path(AliyunOSSUtil.getUrl(ConstantProperties.OSS_BUCKET_NAME, url[1]));
                        list.add(fileLeads);
                        if (updateMap.get("leads_id") != null && updateMap.get("filePath") != null) {
                            System.out.println("");
                            String fileId = fileService.findFiledId(updateMap.get("filePath"));
                            fileLeads.setFile_id(Integer.valueOf(fileId));
                            Map<String, Object> newMap = new HashMap<>();
                            newMap.put("name", filename);
                            newMap.put("size", Integer.valueOf(size.intValue()));
                            newMap.put("file_path", AliyunOSSUtil.getUrl(ConstantProperties.OSS_BUCKET_NAME, url[1]));
                            newMap.put("leads_id", Integer.valueOf(updateMap.get("leads_id")));
                            newMap.put("filePath", updateMap.get("filePath"));
                            insertFile = fileService.updateFiled(newMap);
                        } else {
                            insertFile = this.fileService.insert(list);
                        }
                        newList.add(fileLeads.getFile_id());
                    }
                }
                for (int j = 0; j < fileList.size(); j++) {
                    System.out.println(files.length);
                    Map<String, Object> newMap = new HashMap<>();
                    newMap.put("file_path", fileList.get(j));
                    newMap.put("file_id", newList.get(j));
                    fileIdUrl.add(newMap);
                }
                if (updateMap.get("leads_id") != null) {
                    for (int i = 0; i < fileList.size(); i++) {
                        RFileLeads rFileLeads = new RFileLeads();
                        rFileLeads.setFile_id(newList.get(i));
                        rFileLeads.setLeads_id(Integer.parseInt(updateMap.get("leads_id")));
                        rFileLeads.setCompany_id(getUser().getCompany_id());
                        listFile.add(rFileLeads);
                    }
                    int insertFocus = rFileLeadsService.insertFile(listFile);
                    if (insertFocus > 0) {
                        return Result.success(fileIdUrl, "添加成功！");
                    } else {
                        return Result.error(CodeMsg.ERROR, "添加失败");
                    }
                }
                if (insertFile > 0) {
                    return Result.success(fileIdUrl, "成功！");
                } else {
                    return Result.error(CodeMsg.ERROR, "失败");
                }
            }
        } catch (Exception e) {
            log.error(e.getMessage());
            return Result.error(CodeMsg.ERROR, e.getMessage());
        }
        return Result.error(CodeMsg.ERROR, "文件为空");
    }


    @ApiOperation("线索视频上传")
    @RequestMapping(value = {"/fileVideo"}, method = {RequestMethod.POST})
    public Result fileVideo(@RequestParam("files") MultipartFile[] files, @RequestParam Map<String, String> updateMap) {
        System.out.println(files.length);
        List<RFileLeads> listFile = new ArrayList<>();
        List<FileLeads> list = new ArrayList();
        List<Map<String, Object>> fileIdUrl = new ArrayList();
        FileLeads fileLeads = null;
        Map<String, Object> map = null;
        List<Integer> newList = new ArrayList<>();
        int insertFile = 0;
        try {
            if (files.length > 0) {
                List<String> fileList = new ArrayList();
                for (MultipartFile file : files) {
                    fileLeads = new FileLeads();
                    String filename = file.getOriginalFilename();
                    log.info("上传" + filename);
                    if (!"".equals(filename.trim())) {
                        File newFile = new File(ConstantProperties.PROP_UPLOAD_FOLDER + filename);
                        if (!newFile.getParentFile().exists()) {
                            newFile.getParentFile().mkdirs();
                        }
                        file.transferTo(newFile);

                        String[] url = AliyunOSSUtil.uploadObject2OSS(newFile);
                        fileList.add(AliyunOSSUtil.getUrl(ConstantProperties.OSS_BUCKET_NAME, url[1]));
                        newFile.delete();
                        fileLeads.setCreate_date(DateUtil.timeMillis());
                        fileLeads.setCompany_id(getUser().getCompany_id());
                        fileLeads.setRole_id(getUser().getRole_id());
                        fileLeads.setName(filename);
                        fileLeads.setType("video");
                        fileLeads.setBelongs(0);
                        Long size = Long.valueOf(file.getSize());
                        fileLeads.setSize(Integer.valueOf(size.intValue()));
                        fileLeads.setFile_path(AliyunOSSUtil.getUrl(ConstantProperties.OSS_BUCKET_NAME, url[1]));
                        list.add(fileLeads);
                        if (updateMap.get("leads_id") != null && updateMap.get("filePath") != null) {
                            System.out.println(files.length);
                            String fileId = fileService.findFiledId(updateMap.get("filePath"));
                            fileLeads.setFile_id(Integer.valueOf(fileId));
                            Map<String, Object> newMap = new HashMap<>();
                            newMap.put("name", filename);
                            newMap.put("size", Integer.valueOf(size.intValue()));
                            newMap.put("file_path", AliyunOSSUtil.getUrl(ConstantProperties.OSS_BUCKET_NAME, url[1]));
                            newMap.put("leads_id", Integer.valueOf(updateMap.get("leads_id")));
                            newMap.put("filePath", updateMap.get("filePath"));
                            insertFile = fileService.updateFiled(newMap);
                            System.out.println(files.length);
                        } else {
                            insertFile = this.fileService.insert(list);
                        }
                        newList.add(fileLeads.getFile_id());
                    }
                }
                for (int j = 0; j < fileList.size(); j++) {
                    Map<String, Object> newMap = new HashMap<>();
                    newMap.put("file_path", fileList.get(j));
                    newMap.put("file_id", newList.get(j));
                    System.out.println(files.length);
                    fileIdUrl.add(newMap);
                }
                if (updateMap.get("leads_id") != null) {
                    for (int i = 0; i < fileList.size(); i++) {
                        RFileLeads rFileLeads = new RFileLeads();
                        rFileLeads.setFile_id(newList.get(i));
                        rFileLeads.setLeads_id(Integer.parseInt(updateMap.get("leads_id")));
                        rFileLeads.setCompany_id(getUser().getCompany_id());
                        System.out.println("");
                        listFile.add(rFileLeads);
                    }
                    int insertFocus = rFileLeadsService.insertFile(listFile);
                    if (insertFocus > 0) {
                        return Result.success(fileIdUrl, "添加成功！");
                    } else {
                        return Result.error(CodeMsg.ERROR, "添加失败");
                    }
                }
                if (insertFile > 0) {
                    return Result.success(fileIdUrl, "成功！");
                } else {
                    return Result.error(CodeMsg.ERROR, "失败");
                }
            }
        } catch (Exception e) {
            log.error(e.getMessage());
            return Result.error(CodeMsg.ERROR, e.getMessage());
        }
        return Result.error(CodeMsg.ERROR, "文件为空");
    }

}
